package net.lab1024.smartadmin.module.business.torqueConverter.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lab1024.smartadmin.common.constant.ResponseCodeConst;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.business.torqueConverter.domain.dto.TorqueConverterAddDTO;
import net.lab1024.smartadmin.module.business.torqueConverter.domain.dto.TorqueConverterQueryDTO;
import net.lab1024.smartadmin.module.business.torqueConverter.domain.dto.TorqueConverterUpdateDTO;
import net.lab1024.smartadmin.module.business.torqueConverter.domain.entity.TorqueConverterEntity;
import net.lab1024.smartadmin.module.business.torqueConverter.domain.vo.TorqueConverterExcelVO;
import net.lab1024.smartadmin.module.business.torqueConverter.domain.vo.TorqueConverterVO;
import net.lab1024.smartadmin.module.business.torqueConverter.service.TorqueConverterService;
import net.lab1024.smartadmin.util.SmartRequestTokenUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * [ ����� ]
 *
 * @author �Ζ�
 * @version 1.0
 * @company ��������
 * @copyright (c)  ��������Inc. All rights reserved.
 * @date 2021-11-19 11:18:26
 * @since JDK1.8
 */
@RestController
@Api(tags = {"�����"})
public class TorqueConverterController extends BaseController {

    @Autowired
    private TorqueConverterService torqueConverterService;

    @ApiOperation(value = "��ҳ��ѯ�����",notes = "@author �Ζ�")
    @PostMapping("/torqueConverter/page/query")
    public ResponseDTO<PageResultDTO<TorqueConverterVO>> queryByPage(@RequestBody TorqueConverterQueryDTO queryDTO) {
        if(queryDTO.getCreateUser()!=null){
            queryDTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        }queryDTO.setIsIndexShow(1);
        queryDTO.setMyUserId(SmartRequestTokenUtil.getRequestUserId());
        return torqueConverterService.queryByPage(queryDTO);
    }

    @GetMapping("/torqueConverter/getById/{id}")
    public ResponseDTO<TorqueConverterEntity> getById(@PathVariable Long id) {
        return ResponseDTO.succData(torqueConverterService.getById(id));
    }

    @ApiOperation(value = "��ӱ����",notes = "@author �Ζ�")
    @PostMapping("/torqueConverter/add")
    public ResponseDTO<String> add(@RequestBody @Validated TorqueConverterAddDTO addTO){
        addTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        addTO.setCreateTime(new Date());
        if(torqueConverterService.select(addTO.getSn(),null)){
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_SN_REPEAT);
        }
        return torqueConverterService.add(addTO);
    }

    @ApiOperation(value="�޸ı����",notes = "@author �Ζ�")
    @PostMapping("/torqueConverter/update")
    public ResponseDTO<String> update(@RequestBody @Validated TorqueConverterUpdateDTO updateDTO){
        if(torqueConverterService.select(updateDTO.getSn(),updateDTO.getId())){
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_SN_REPEAT);
        }
        return torqueConverterService.update(updateDTO);
    }

    @ApiOperation(value="����ɾ�������",notes = "@author �Ζ�")
    @PostMapping("/torqueConverter/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return torqueConverterService.deleteByIds(idList);
    }

    @ApiOperation(value = "��������", notes = "@author �Ζ�")
    @PostMapping("/torqueConverter/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //��ѯ����
        List<TorqueConverterExcelVO> torqueConverterList = torqueConverterService.queryBatchExportData(idList);
        //��������
        ExportParams ex = new ExportParams("�����", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, TorqueConverterExcelVO.class, torqueConverterList);
        downloadExcel("�����", workbook, response);
    }

    @ApiOperation(value = "����ȫ��", notes = "@author �Ζ�")
    @PostMapping("/torqueConverter/export/all")
    public void exportAll(@RequestBody @Validated TorqueConverterQueryDTO queryDTO, HttpServletResponse response) {
        //��ѯ����
        List<TorqueConverterExcelVO> torqueConverterList = torqueConverterService.queryAllExportData(queryDTO);
        //��������
        ExportParams ex = new ExportParams("�����", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, TorqueConverterExcelVO.class, torqueConverterList);
        downloadExcel("�����", workbook, response);
    }

}
